package com.zhz.project.main.doaj;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhz.project.dal.daointerface.TocDoajDAO;
import com.zhz.project.dal.dataobject.TocDoaj;
import com.zhz.project.util.getWebContent;

public class DoajTocMain implements Runnable {

    String             url = "http://www.doaj.org/doaj?func=abstract&id=";

    private int        index;
    private TocDoajDAO tocDoajDAO;

    public DoajTocMain(int index, TocDoajDAO tocDoajDAO) {
        this.index = index;
    }

    public static void main(String[] args) {
        ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
            new String[] { "ibatis-spring.xml" });

        TocDoajDAO tocDoajDAO = (TocDoajDAO) appContext.getBean("tocDoajDAO");

        for (int index = 0; index < 11; index++) {
            Thread t = new Thread(new DoajTocMain(index, tocDoajDAO));
            t.start();
        }
    }

    public void run() {
        for (int i = 5000 * index; i < 5000 * (index + 1); i++) {
            TocDoaj td = parse(url + i, i);
            if (td.getTitle() != null) {
                if (!td.getTitle().equals("")) {
                    td.setSourceurl(url + i);
                    tocDoajDAO.insertTocDoaj(td);
                }
            }
            System.out.println(i);
        }

    }

    public TocDoaj parse(String url, int index) {

        String content = null;

        content = getWebContent.getContent(url);
        DoajParser dp = new DoajParser();
        return dp.parse(content, index);

    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}
